﻿using BZ.Logger;
using NDK.Frm;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace NDK.Module.BLL
{
    public class Station00CtLogger
    {
        /// <summary>
        /// 组合日志对象
        /// </summary>
        private static CompositeLogger compositeLogger = null;
        /// <summary>
        /// 文件日志对象
        /// </summary>
        private static FileLogger fileLogger = null;
        /// <summary>
        /// 界面日志对象
        /// </summary>
        //private static LogBoardLogger dataGridViewLogBoardLogger = null;
        private static DataGridViewLogBoardLogger dataGridViewLogBoardLogger = null;

        /// <summary>
        /// 配置日志对象
        /// </summary>
        public static BZLogger Config()
        {
            BZLogger logger = new BZLogger();
            try
            {
                //配置文件日志
                string strPath = Application.StartupPath + "\\log\\CtLog\\Station00\\";
                if (!Directory.Exists(strPath))
                {
                    Directory.CreateDirectory(strPath);
                }
                if (fileLogger == null) fileLogger = new FileLogger(strPath, "", ".log");
                fileLogger.IntervalMode = true;
                fileLogger.ActiveFileLog = true;
                fileLogger.LogDay = 7;
                fileLogger.LogLevel = 1;

                //配置界面日志
                if (dataGridViewLogBoardLogger == null)
                {
                    Thread.Sleep(500);
                    dataGridViewLogBoardLogger = new DataGridViewLogBoardLogger(Frm_Home.Instance.dataGridViewLogBoard1);
                }

                //dataGridViewLogBoardLogger = new LogBoardLogger(Frm_Home.Instance.logBoard1);
                dataGridViewLogBoardLogger.LogLevel = 1;
                dataGridViewLogBoardLogger.MaxCount = 500;

                //组合多个日志，同时输出到多个日志对象
                if (compositeLogger == null)
                {
                    compositeLogger = new CompositeLogger(dataGridViewLogBoardLogger, fileLogger);
                    compositeLogger.Initialize();
                    //Frm_Home.Instance.Logger.OnLog += new LogEventHandler(LogNotifier_OnLog);
                    logger.OnLog += new LogEventHandler(LogNotifier_OnLog);
                }
            }
            catch (Exception ex)
            {
                //Common.AppendUiLog.Log( "设置[日志参数]错误 [" + ex.Message + "]",LogType.Error, "配置信息", "");
                MessageBox.Show("设置[日志参数]错误 [" + ex.Message + "]");
            }
            return logger;
        }
        private static void LogNotifier_OnLog(object sender, LogEventArgs e)
        {
            if (compositeLogger != null)
            {
                compositeLogger.LogMessage(e);
            }
        }
    }
}
